package tech.bmatch.meituan;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.bmatch.meituan.model.Dishes;
import tech.bmatch.meituan.model.Merchant;
import tech.bmatch.meituan.model.MerchantSearchParam;
import tech.bmatch.meituan.service.MerchantService;
import tech.bmatch.meituan.service.impl.MerchantServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;

public class Application {
    private static MerchantService merchantService = new MerchantServiceImpl();
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        logger.info("启动应用");
        merchantService.init();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(System.in));
        System.out.println("请输入指令：");
        System.out.println("添加商家的指令：add 商户ID 商户名称 经度 纬度");
        System.out.println("查询商家的指令：search 商户名称[可选]");
        System.out.println("展示商家的指令：display ");

        while (true) {
            String command = null;
            try {
                command = bufferedReader.readLine();
            } catch (IOException e) {
                logger.error("", e);
            }
            logger.info("执行命令：" + command);

            if (command.startsWith("add ")) {
                String content = command.replaceAll("add ", "");
                initMerchant(content.split(" "));
            }
            if (command.startsWith("search ")) {
                String content = command.replaceAll("search ", "");
                search(content);
            }
            if (command.startsWith("display")) {
                String content = command.replaceAll("display ", "");

            }
        }
        //南大金陵 经纬度118.706076，32.176591

    }

    private static void search(String name) {
        MerchantSearchParam param = new MerchantSearchParam();
        param.setName(name);
        param.setLon(118.706076);
        param.setLat(32.176591);
        merchantService.search(param);
        Set<Merchant> merchantList = merchantService.search(param);
        for (Merchant merchant : merchantList) {
            System.out.println("商家名称:" + merchant.getName());
            System.out.println("距离" + merchant.getDistance() + "m");
            System.out.println("-----------------------");
        }
    }


    private static void initMerchant(String[] items) {

        Merchant merchant = new Merchant();
        merchant.setId(items[0]);
        merchant.setName(items[1]);
        merchant.setLon(Double.valueOf(items[2]));
        merchant.setLat(Double.valueOf(items[3]));
        merchantService.add(merchant);

    }

    private static void initDishes(String[] items) {
        Dishes dishes = new Dishes();
        dishes.setId(items[0]);
        dishes.setMerchantName(items[1]);
        dishes.setDishesName(items[1]);
        dishes.setServing(items[2]);
    }
}
